from threading import Thread
from queue import Queue


class Producer(Thread):

    def __init__(self, name: str, count: int, queue: Queue) -> None:
        super(Producer, self).__init__()

        # 线程自带属性
        self.name = name

        # 自定义属性
        self.count = count
        self.queue = queue
        

    def run(self) -> None:
        for n in range(self.count):
            msg = f"{self.name} - {n}"
            self.queue.put(msg, block=True)


class Consumer(Thread):

    def __init__(self, name: str, queue: Queue) -> None:
        super().__init__()

        self.name = name
        self.daemon = True

        self.queue = queue

    def run(self) -> None:
        while True:
            msg = self.queue.get(block=True)
            print(f"{self.name} - {msg}\n", end="")


queue = Queue(maxsize=3)
threads = [
    Producer("p1", 10, queue),
    Producer("p2", 10, queue),
    Producer("p3", 10, queue),

    Consumer("c1", queue),
    Consumer("c2", queue),
]

for t in threads:
    t.start()
